﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Ue.CQRS.Samples.Infrastructure.Data;

namespace Ue.CQRS.Samples.Application.Query.Repositories
{
    public class ConcurrentRepository<T> :System.Collections.Concurrent.ConcurrentDictionary<Guid,T>,IRepository<Guid,T>
    {
        public T Remove(Guid key)
        {
            T value = default(T);

            base.TryRemove(key, out value);

            return value;
        }

        public IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate, int? skip, int? count)
        {
            var results = base.Values.Where(predicate.Compile());

            if (skip != null)
            {
                results = results.Skip(skip.Value);
            }

            if (count != null)
            {
                results = results.Take(count.Value);
            }

            return results;
        }
    }
}